package com.xh;

import com.xh.common.consts.CommonConst;
import com.xh.common.utils.JsonUtil;
import com.xh.doamin.es.EsArticle;
import com.xh.doamin.vo.PageResult;
import com.xh.repository.EsArticleRepository;
import com.xh.service.IEsArticleService;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.SQLOutput;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest01 {

    @Autowired
    private IEsArticleService esArticleService;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    public void test01() {
        String keyword = "spring";
        SearchQuery query = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.matchQuery(CommonConst.ARTICLE_TITLE_KEY, keyword).operator(Operator.OR))
                .withQuery(QueryBuilders.matchQuery(CommonConst.ARTICLE_SUMMARY_KEY, keyword).operator(Operator.OR))
                .withPageable(PageRequest.of(0, 10))
                .build();
        AggregatedPage<EsArticle> result = elasticsearchTemplate.queryForPage(query, EsArticle.class);
        System.out.println(JsonUtil.serialize(new PageResult(result.getTotalElements(), result.getNumber() + 1, result.getTotalPages(), result.getContent())));
    }

    @Test
    public void test02() {
        System.err.println(JsonUtil.serialize(esArticleService.search("安装", 1, 10)));
    }

}
